package com.server.llh.llhserverwork.activity.food;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.lelaohui.net.BaseLeLaohuiAcitivity;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetRequestManager;
import com.lelaohui.net.request.LRequest;
import com.lelaohui.net.utils.UrlsTools;

import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.adapter.FoodAdapter;
import com.server.llh.llhserverwork.adapter.LLHOrderFoodTypeAdapter;
import com.server.llh.llhserverwork.adapter.LLHShoppingFoodAdapter;
import com.server.llh.llhserverwork.bean.Constants;

import com.server.llh.llhserverwork.bean.FoodInfoBean;
import com.server.llh.llhserverwork.bean.FoodInfoListBean;
import com.server.llh.llhserverwork.bean.FoodOrderInfoListBean;
import com.server.llh.llhserverwork.bean.MyOrgInfoData;
import com.server.llh.llhserverwork.bean.ShowProInfoBean;

import com.server.llh.llhserverwork.bean.WapFoodTemplate;
import com.server.llh.llhserverwork.port.ClearViewDataInterface;

import com.server.llh.llhserverwork.port.OrderFoodInterface;
import com.server.llh.llhserverwork.utils.FoodDataSetting;
import com.server.llh.llhserverwork.utils.ServerPort;
import com.server.llh.llhserverwork.utils.SharedPreferenceUtil;
import com.server.llh.llhserverwork.utils.ToastTool;
import com.server.llh.llhserverwork.view.MyPopupWindow;
import com.server.llh.llhserverwork.view.ShowProInfoView;
import com.server.llh.llhserverwork.view.popwindow.MyPopWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LlhFoodInfoActivity extends BaseLeLaohuiAcitivity implements OrderFoodInterface, MyPopWindow.ItemClickCallBack, ClearViewDataInterface {
    private final static String TAG = "LlhFoodInfoActivity";
    @BindView(R.id.main_img)
    ImageView main_img;
    @BindView(R.id.left_btn)
    ImageButton leftBtn;
    @BindView(R.id.titlebar_text)
    TextView titlebarText;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.today_view)
    RadioButton todayView;
    @BindView(R.id.tomorrow_view)
    RadioButton tomorrowView;
    @BindView(R.id.after_tomorrow_view)
    RadioButton afterTomorrowView;
    @BindView(R.id.select_radio_group)
    RadioGroup selectRadioGroup;
    @BindView(R.id.morning_view)
    RadioButton morningView;
    @BindView(R.id.noon_view)
    RadioButton noonView;
    @BindView(R.id.night_view)
    RadioButton nightView;
    @BindView(R.id.select_time_group)
    RadioGroup selectTimeGroup;
    @BindView(R.id.food_time)
    LinearLayout foodTime;
    @BindView(R.id.menu_two_lv)
    ListView menuTwoLv;
    @BindView(R.id.menu_content_lv)
    ListView menuContentLv;
    @BindView(R.id.noDataSrc)
    ImageView noDataSrc;
    @BindView(R.id.noData)
    RelativeLayout noData;
    @BindView(R.id.center)
    LinearLayout center;
    @BindView(R.id.food_info)
    LinearLayout foodInfo;
    @BindView(R.id.shop_car_total_msg)
    TextView shopCarTotalMsg;
    @BindView(R.id.shop_car_commit)
    Button shopCarCommit;
    @BindView(R.id.msg_toast)
    ImageView msgToast;
    @BindView(R.id.food_num)
    TextView foodNum;
    @BindView(R.id.shop_car_layout)
    RelativeLayout shopCarLayout;
    @BindView(R.id.buttom_view)
    LinearLayout buttomView;
    public int mealTime = 1;
    String cacheKey = Constants.CACHE_KEY.FOOT_TODAY_KEY;
    private final static String BREAK_FOOD = "1";
    private final static String TODAY_FOOD = "0";
    private final static String TOMORROW_FOOD = "1";
    private final static String POSTNATAL_FOOD = "2";
    private String isScope = TODAY_FOOD;

    public String curMealTime = BREAK_FOOD;
    //食品信息
    private ArrayList<FoodInfoBean> totleFoodList = new ArrayList<>();
    /**
     * 当前食物列表
     */
    private ArrayList<FoodInfoBean> curFoodList = new ArrayList<>();
    private LLHOrderFoodTypeAdapter llhOrderFoodTypeAdapter = null;
    private String curFoodType, mUserId, intentTag, orgType, orgId, token;//当前食物类型
    /**
     * 食物类型数组
     */
    ArrayList<String> foodType = new ArrayList<String>();
    /**
     * 购物车食物列表
     */
    ArrayList<FoodInfoBean> shopping_cart_List = new ArrayList<FoodInfoBean>();
    FoodAdapter foodAdapter;
    MyPopWindow.ItemClickCallBack itemClickCallBack;
    boolean isScroll = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        itemClickCallBack = this;
        SubFoodOrderInfoActivity.registCallBack(this);
        orgId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_ID, getApplicationContext());
        orgType = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.ORG_TYPE, getApplicationContext());
        mUserId = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.USER_ID, getApplicationContext());
        token = SharedPreferenceUtil.getUserInfo(Constants.UserInfo.TOKEN_STR, getApplicationContext());
        if (getIntent() != null) {
            intentTag = getIntent().getStringExtra("intentTag");
        }
        FoodDataSetting.getDate();
        setData(null);
        rightBtn.setBackgroundResource(R.mipmap.llh_org_mour);
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if ("0".equals(intentTag)) {
                    if (LlhFoodServerTitleActivity.myOrgInfoDataList != null && LlhFoodServerTitleActivity.myOrgInfoDataList.size() > 0) {
                        myOrgInfoDataList = LlhFoodServerTitleActivity.myOrgInfoDataList;
                        setPoPuData(myOrgInfoDataList);
                    } else {
                    }
                } else {
                    if (myOrgInfoDataList != null && myOrgInfoDataList.size() > 0) {
                        setPoPuData(myOrgInfoDataList);
                    }
                }
            }
        });

        buttomView.setVisibility(View.GONE);
        foodAdapter = new FoodAdapter(LlhFoodInfoActivity.this);
        menuContentLv.setAdapter(foodAdapter);
        foodAdapter.registerCallBack(this);

        morningView.setBackgroundResource(R.color.top_color);
        noonView.setBackgroundResource(R.color.top_color);
        nightView.setBackgroundResource(R.color.top_color);

        gerFoodInfo(isScope, orgId, orgType);
        selectRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.today_view) {
                    if (isScope != TODAY_FOOD && shopping_cart_List.size() > 0) {
                        setisScope(TODAY_FOOD, Constants.CACHE_KEY.FOOT_TODAY_KEY);
                    } else {
                        cacheKey = Constants.CACHE_KEY.FOOT_TODAY_KEY;
                        isScope = TODAY_FOOD;
                        Log.d(TAG, "one time ==" + isScope);
                        gerFoodInfo(isScope, orgId, orgType);
                    }
                } else if (checkedId == R.id.tomorrow_view) {
                    if (isScope != TOMORROW_FOOD
                            && shopping_cart_List.size() > 0) {
                        setisScope(TOMORROW_FOOD, Constants.CACHE_KEY.FOOT_TOMORROW_KEY);
                    } else {
                        Log.d(TAG, "two time ==" + isScope);

                        cacheKey = Constants.CACHE_KEY.FOOT_TOMORROW_KEY;
                        isScope = TOMORROW_FOOD;
                        gerFoodInfo(isScope, orgId, orgType);
                    }
                } else {
                    if (isScope != POSTNATAL_FOOD
                            && shopping_cart_List.size() > 0) {
                        setisScope(POSTNATAL_FOOD,
                                Constants.CACHE_KEY.FOOT_AFTERTOMORROW_KEY);
                    } else {
                        Log.d(TAG, "three time ==" + isScope);
                        cacheKey = Constants.CACHE_KEY.FOOT_AFTERTOMORROW_KEY;
                        isScope = POSTNATAL_FOOD;
                        gerFoodInfo(isScope, orgId, orgType);
                    }
                }
            }
        });
        selectTimeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.morning_view) {
                    setSelectTimeFood(1);
                } else if (checkedId == R.id.noon_view) {
                    setSelectTimeFood(2);
                } else if (checkedId == R.id.night_view) {
                    setSelectTimeFood(3);
                }
            }
        });
        menuContentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (curFoodList.get(position) != null) {
                    ShowProInfoBean showProInfoBean = new ShowProInfoBean(curFoodList.get(position).getProPic(),
                            curFoodList.get(position).getProPrice(), curFoodList.get(position).getProName(),
                            curFoodList.get(position).getRemark(), "100%");
                    ShowProInfoView.showProInfo(showProInfoBean, LlhFoodInfoActivity.this.findViewById(R.id.shop_main), LlhFoodInfoActivity.this);
                }
            }
        });
        menuTwoLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                isScroll = false;
                setLeftSelecter(position);
            }
        });
    }
    MyPopWindow popWindow = null;
    private void setPoPuData(List<MyOrgInfoData.DataBean> myOrgInfoDataList) {
        popWindow = new MyPopWindow(LlhFoodInfoActivity.this, myOrgInfoDataList, itemClickCallBack);
        popWindow.showPopupWindow(LlhFoodInfoActivity.this.findViewById(R.id.right_btn));
    }

    private void setData(final ArrayList<FoodInfoBean> totleFoodList) {
        llhOrderFoodTypeAdapter = new LLHOrderFoodTypeAdapter();
        menuTwoLv.setAdapter(llhOrderFoodTypeAdapter);
        initLeftData(curFoodType);
        initShopingCarView();
    }

    private RelativeLayout buy_list_Layout;
    TextView totle_price, delete_all;
    ListView buy_listView;

    public void initShopingCarView() {
        buy_list_Layout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.llh_shopping_list_layout, null);
        totle_price = (TextView) buy_list_Layout.findViewById(R.id.totle_price);
        delete_all = (TextView) buy_list_Layout.findViewById(R.id.delete_all);
        delete_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showInfo();
            }
        });
        buy_listView = (ListView) buy_list_Layout.findViewById(R.id.pro_listview);
        llhShoppingFoodAdapter = new LLHShoppingFoodAdapter();
        buy_listView.setAdapter(llhShoppingFoodAdapter);
        llhShoppingFoodAdapter.registerCallBack(LlhFoodInfoActivity.this);
    }

    LLHShoppingFoodAdapter llhShoppingFoodAdapter;
    /**
     * 点击显示购物车popuwindow
     */
    private MyPopupWindow myPopupWindow = null;

    public void showShopProPopView() {
        if (shopping_cart_List == null || shopping_cart_List.size() == 0) {
            ToastTool.showText(getApplicationContext(), "抱歉，您还未选择餐品！");
            return;
        } else if (shopping_cart_List != null && shopping_cart_List.size() > 0) {
            myPopupWindow = new MyPopupWindow(LlhFoodInfoActivity.this, buy_list_Layout);
            myPopupWindow.showAtLocation(LlhFoodInfoActivity.this.findViewById(R.id.shop_main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            llhShoppingFoodAdapter.setData(shopping_cart_List);
        }
    }

    /**
     * 选择早，午，晚 餐品
     *
     * @param mealTime
     */
    private void setSelectTimeFood(int mealTime) {
        if (shopping_cart_List != null && shopping_cart_List.size() == 0) {
            this.mealTime = mealTime;
            initRightListData("" + mealTime);
        } else {
            cleanDayFood();
        }
    }

    private void cleanDayFood() {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                LlhFoodInfoActivity.this);
        builder.setMessage("您当前顿已选购了餐饮，是否要放弃？");
        builder.setTitle("温馨提示");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                clearListData();
                dialog.cancel();
                ToastTool.showText(getApplicationContext(), "请选择其他时间的餐品!");

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ToastTool.showText(getApplicationContext(), "请您继续选购!");
                dialog.cancel();
            }
        });
        builder.create().show();
    }

    public void initLeftData(String mealTime) {
        getFoodType();
        llhOrderFoodTypeAdapter.setData(foodType);
        new Handler().postAtTime(new Runnable() {
            @Override
            public void run() {
                menuTwoLv.setSelected(true);
                menuTwoLv.setSelection(0);
                menuTwoLv.setItemChecked(0, true);
                menuTwoLv.performItemClick(menuTwoLv.getChildAt(0), 0, 0);
                setLeftSelecter(0);
            }
        }, 500);
        if (foodType == null || foodType.size() == 0) {
            return;
        }
        ArrayList<FoodInfoBean> data = new ArrayList<FoodInfoBean>();
        if (data == null || data.size() == 0) {
            return;
        }
        Log.d(TAG, "mealTime==" + mealTime);
        data = sortFood(foodType.get(0), mealTime);
        foodAdapter.setData(data, isScope);
        foodAdapter.notifyDataSetChanged();
    }

    public void setLeftSelecter(int leftSelecter) {
        if (foodType != null && foodType.size() == 0) {
            getFoodType();
            return;
        } else {
            curFoodType = foodType.get(leftSelecter);
        }
        initRightListData(curMealTime);
    }

    /**
     * 初始化右侧数据
     *
     * @param mealTime
     */
    private void initRightListData(String mealTime) {
        Log.d(TAG, "curMealTime===" + curMealTime);
        curMealTime = "" + mealTime;
        ArrayList<FoodInfoBean> data = sortFood(curFoodType, mealTime);
        foodAdapter.setData(data, isScope);
        foodAdapter.notifyDataSetChanged();
    }

    /**
     * 根据食品类型和吃饭时间划分
     *
     * @param cateName
     * @param mealTime
     */
    public ArrayList<FoodInfoBean> sortFood(String cateName, String mealTime) {
        curFoodList.clear();
        for (int i = 0; i < totleFoodList.size(); i++) {
            FoodInfoBean food = totleFoodList.get(i);
            Log.d(TAG, " food.getMealTime()==" + food.getMealTime() + "cateName==" + cateName + "mealTime==" + mealTime);
            if (food.getCateName().equals(cateName)
                    && Integer.valueOf(food.getMealTime()) == Integer.valueOf(mealTime)) {
                curFoodList.add(food);
            }
        }
        return curFoodList;
    }

    /**
     * 获取食物的类型
     */
    public ArrayList<String> getFoodType() {
        foodType.clear();
        for (int i = 0; i < totleFoodList.size(); i++) {
            FoodInfoBean food = totleFoodList.get(i);
            if (!foodType.contains(food.getCateName())) {
                foodType.add(food.getCateName());
            }
        }
        if (foodType.size() > 0) {
            curFoodType = foodType.get(0);
        }
        return foodType;
    }

    /**
     * 清楚今日所选餐品，清楚购物车数据
     */
    public void clearListData() {
        for (int i = 0; i < shopping_cart_List.size(); i++) {
            FoodInfoBean foodModel = shopping_cart_List.get(i);
            foodModel.buyNum = 0;
        }
        foodAdapter.notifyDataSetChanged();
        shopping_cart_List.clear();
        llhShoppingFoodAdapter.notifyDataSetChanged();
        refreshShoppingList(null);
        if (myPopupWindow != null) {
            myPopupWindow.dismiss();
        }
    }

    protected int getlayout() {
        return R.layout.llh_food_info;
    }

    @OnClick({R.id.left_btn, R.id.main_img, R.id.shop_car_commit, R.id.msg_toast, R.id.food_num, R.id.shop_car_layout, R.id.buttom_view})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.left_btn:
                onBackPressed();
                break;
            case R.id.shop_car_commit:
//                  intent=new Intent(getApplicationContext(),SubFoodOrderInfoActivity.class);
//                startActivity(intent);
                createOrderSort();
                break;
            case R.id.msg_toast:
                showShopProPopView();
                break;
            case R.id.food_num:
                break;
            case R.id.shop_car_layout:
                break;
            case R.id.buttom_view:
                break;
            case R.id.main_img:
                intent = new Intent();
                intent.setAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
                sendBroadcast(intent);
                onBackPressed();
                break;
        }
    }

    @Override
    public void callBack() {

    }

    public String moneyConunt;

    @Override
    public void refreshShoppingList(FoodInfoBean foodModel) {
        int num = getNumData(foodModel);
        foodNum.setText("" + num);
        if (shopping_cart_List.size() > 0 && num > 0) {
            buttomView.setVisibility(View.VISIBLE);
            float totlepeice = 0;
            for (int i = 0; i < shopping_cart_List.size(); i++) {
                FoodInfoBean tempFoodModel = shopping_cart_List.get(i);
                totlepeice = (float) (totlepeice + tempFoodModel.getProPrice()
                        * tempFoodModel.buyNum);
            }
            double d = totlepeice;
            String result = String.format("%.2f", d);
            shopCarTotalMsg.setText("总金额：" + result + "元");
            totle_price.setText("总金额：" + result + "元");
            moneyConunt = result;
        } else {
            shopCarTotalMsg.setVisibility(View.VISIBLE);
            shopCarTotalMsg.setText("请为老人选择餐品");
            totle_price.setText("总金额：0元");

        }
        Log.d(TAG, "shopping_cart_List.size==" + shopping_cart_List.size());
        llhShoppingFoodAdapter.setData(shopping_cart_List);
        llhShoppingFoodAdapter.notifyDataSetChanged();
        if (myPopupWindow != null && myPopupWindow.isShowing()) {
            myPopupWindow.dismiss();
        }
    }

    private int getNumData(FoodInfoBean foodModel) {
        boolean isAdded = false;
        for (int i = 0; i < shopping_cart_List.size(); i++) {
            FoodInfoBean tempFoodModel = shopping_cart_List.get(i);
            if (foodModel.getProId().equals(tempFoodModel.getProId())) {
                isAdded = true;
                if (foodModel.buyNum <= 0) {
                    shopping_cart_List.remove(i);
                    i--;
                }
                tempFoodModel.buyNum = foodModel.buyNum;
                break;
            }
        }
        if (!isAdded && foodModel != null) {
            boolean isAdded2 = false;
            for (int i = 0; i < shopping_cart_List.size(); i++) {
                FoodInfoBean tempFoodModel = shopping_cart_List.get(i);
                if (tempFoodModel.getMealTime() == foodModel.getMealTime()) {
                    shopping_cart_List.add(foodModel);
                    isAdded2 = true;
                    break;
                }
            }
            if (!isAdded2) {
                shopping_cart_List.add(foodModel);
            }
        }
        int num = 0;
        for (int i = 0; i < shopping_cart_List.size(); i++) {
            FoodInfoBean tempFoodModel = shopping_cart_List.get(i);
            num = num + tempFoodModel.buyNum;
        }
        return num;
    }

    @Override
    public void refreshFoodList() {
        foodAdapter.notifyDataSetChanged();
    }

    public void setisScope(String isScopeStr, final String cacheKeyStr) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("温馨提示");
        builder.setMessage("\n" + "您有当日已选购的餐饮，是否要放弃？" + "\n");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                shopping_cart_List.clear();
                refreshShoppingList(null);
                cacheKey = cacheKeyStr;
                isScope = TODAY_FOOD;
                gerFoodInfo(isScope, orgId, orgType);
                dialog.cancel();
            }
        });
        builder.show();

    }

    private void showInfo() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("温馨提示");
        builder.setMessage("\n" + "您确定要清空购物车里的商品么？" + "\n");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ToastTool.showText(getApplicationContext(), "请您继续选购！");
                dialog.cancel();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                clearListData();
                dialog.cancel();
            }
        });
        builder.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (curFoodList != null) {
            curFoodList.clear();
        }
    }

    /**
     * 获取餐品信息请求
     *
     * @param isScope
     */
    FoodInfoListBean foodInfoListBean;

    private void gerFoodInfo(String isScope, String orgId, String orgType) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("orgId", orgId);
        lRequest.addParam("orgType", orgType);
        lRequest.addParam("isScope", isScope);
        lRequest.addParam("token", token);
        lRequest.setIp(UrlsTools.REQ_URL);
        lRequest.setUri(ServerPort.FOOD_JSON_DATA);
        lRequest.setUriType(UrlsTools.FOOD_INFO);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), getResources().getString(R.string.toast_str));
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null && Constants.RESPON_SUCC_CODE.equals(code)) {
                    Log.d(TAG, "responseBean==" + responseBean);
                    foodInfoListBean = new Gson().fromJson(responseBean, FoodInfoListBean.class);
                    if (foodInfoListBean != null) {
                        totleFoodList = foodInfoListBean.getData();
                        if (totleFoodList != null) {
                            Log.d(TAG, "totleFoodList===" + totleFoodList.size());
                            setData(totleFoodList);
                        }
                    }
                } else {
                    T.showAnimToast(getApplicationContext(), "暂时没有餐品信息！");
                }
            }
        });
    }

    /**
     * 下单获取值
     */
    public void createOrderSort() {
        ArrayList<ArrayList<FoodInfoBean>> foodTimeList = new ArrayList<ArrayList<FoodInfoBean>>();
        for (int i = 0; i < shopping_cart_List.size(); i++) {
            FoodInfoBean foodModel = shopping_cart_List.get(i);
            if (foodTimeList.size() == 0) {
                ArrayList<FoodInfoBean> foodTypeList = new ArrayList<FoodInfoBean>();
                foodTypeList.add(foodModel);
                foodTimeList.add(foodTypeList);
            } else {
                boolean isHave = false;
                for (int j = 0; j < foodTimeList.size(); j++) {
                    ArrayList<FoodInfoBean> typeList = foodTimeList.get(j);
                    if (foodModel.getMealTime() == typeList.get(0).getMealTime()) {
                        typeList.add(foodModel);
                        isHave = true;
                        break;
                    }
                }
                if (!isHave) {
                    ArrayList<FoodInfoBean> foodTypeList = new ArrayList<FoodInfoBean>();
                    foodTypeList.add(foodModel);
                    foodTimeList.add(foodTypeList);
                }
            }
        }
        for (int i = 0; i < foodTimeList.size(); i++) {
            ArrayList<FoodInfoBean> shopping_List = foodTimeList.get(i);
            if (shopping_List.size() > 0) {
                if (i == shopping_List.size() - 1) {
                    crateFoodOrderData(shopping_List, true);
                } else {
                    crateFoodOrderData(shopping_List, false);
                }
            }
        }
        if (foodTimeList.size() == 0) {
            ToastTool.showText(getApplicationContext(), "您还未选择餐品!");
            return;
        }
    }

    private void crateFoodOrderData(ArrayList<FoodInfoBean> shopping_cart_List, boolean isLast) {
        try {
            /**
             * 获取订单值
             */
            String mealTime = String.valueOf(shopping_cart_List.get(0).getMealTime());
            ArrayList<WapFoodTemplate> data = new ArrayList<WapFoodTemplate>();
            for (int i = 0; i < shopping_cart_List.size(); i++) {
                FoodInfoBean foodModel = shopping_cart_List.get(i);
                WapFoodTemplate bundle = new WapFoodTemplate();
                bundle.setProId(foodModel.getProId());
                bundle.setCateId(foodModel.getCateId());
                bundle.setProNum(foodModel.buyNum + "");
                bundle.setMealType(foodModel.getMealType());
                bundle.setMealTime(foodModel.getMealTime());
                bundle.setProPic(foodModel.getProPic());
                bundle.setSupplierType(foodModel.getSupplierType());
                bundle.setSupplierId(foodModel.getSupplierId());
                bundle.setSupplierName(foodModel.getSupplierName());
                data.add(bundle);
            }
            if (isLast) {
                createFoodOrderInfo(mUserId, isScope, mealTime, data);
            } else {
                createFoodOrderInfo(mUserId, isScope, mealTime, data);
            }
        } catch (Exception e) {
        }
    }

    public static FoodOrderInfoListBean foodOrderInfoListBean;

    private void createFoodOrderInfo(String userId, final String isScope, final String mealTime, ArrayList<WapFoodTemplate> data) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("userId", userId);
        lRequest.addParam("mealTime", mealTime);
        String json = new Gson().toJson(data);
        Log.d(TAG, "orderData==" + json);
        lRequest.addParam("list", json);
        lRequest.addParam("isScope", isScope);
        lRequest.addParam("token", token);
        lRequest.addParam("isDistr", 1);
        lRequest.setReqType("");
        lRequest.setIp(UrlsTools.REQ_URL);
        lRequest.setUri(ServerPort.CONFIRM_FOOD_JSON_DATA);
        lRequest.setUriType(UrlsTools.FOOD_INFO);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), getResources().getString(R.string.toast_str));
            }

            @Override
            public void onNext(String responseBean, String code) {
                if (responseBean != null && Constants.RESPON_SUCC_CODE.equals(code)) {
                    Log.d(TAG, "responseBean==" + responseBean);
                    foodOrderInfoListBean = new Gson().fromJson(responseBean, FoodOrderInfoListBean.class);
                    if (foodOrderInfoListBean != null) {
                        Intent intent = new Intent(getApplicationContext(), SubFoodOrderInfoActivity.class);
                        intent.putExtra("isScope", isScope);
                        intent.putExtra("mealTime", mealTime);
                        Log.d(TAG, "isScope==" + isScope + "mealTime==" + mealTime);
                        startActivity(intent);
                    }
                } else {
                    T.showAnimToast(getApplicationContext(), "提交购物车失败，请重新提交！");
                }
            }
        });
    }


    public void showProgress() {
        try {
            showProgress(LlhFoodInfoActivity.this);
        } catch (Exception e) {

        }
    }

    public void hideProgress() {
        try {
            closeProgress();
        } catch (Exception e) {

        }
    }

    protected INetRequestManager getRequestManager() {
        return NetRequestManager.getInstance();
    }


    @Override
    protected void initView() {

    }

    @Override
    protected void doBasess() {

    }

    String longtitude, latitude;

    @Override
    public void CallBack(MyOrgInfoData.DataBean myOrgInfoData) {
        if (myOrgInfoData != null && !"0".equals(myOrgInfoData.getOrgId())) {
            orgId = myOrgInfoData.getOrgId();
            orgType = myOrgInfoData.getOrgType() + "";
            gerFoodInfo(isScope, myOrgInfoData.getOrgId(), myOrgInfoData.getOrgType() + "");
        } else {
            if (getIntent() != null) {
                longtitude = getIntent().getStringExtra("longtitude");
                latitude = getIntent().getStringExtra("latitude");
                getOrgContent(longtitude, latitude);
            }
        }
    }

    public static List<MyOrgInfoData.DataBean> myOrgInfoDataList = new ArrayList<>();
    MyOrgInfoData myOrgInfoData = null;

    public void getOrgContent(String longtitude, String latitude) {
        LRequest lRequest = new LRequest();
        lRequest.addParam("token", token);
        lRequest.addParam("orgId", orgId);
        if (latitude != null && longtitude != null) {
            lRequest.addParam("latitude", latitude);
            lRequest.addParam("longitude", longtitude);
        }
        lRequest.setUri(ServerPort.GET_CANTINE_LIST);
        lRequest.setType(String.class);
        requestData(lRequest, new NetDataListener<String>() {
            @Override
            public void onError(Throwable t) {
                T.showAnimErrorToast(getApplicationContext(), getResources().getString(R.string.toast_str));
            }

            @Override
            public void onNext(String responseBean, String code) {
                if ("0".equals(code) && responseBean != null) {
                    Log.d("responseBean", "responseBean==" + responseBean);
                    try {
                        myOrgInfoData = new Gson().fromJson(responseBean, MyOrgInfoData.class);
                        if (myOrgInfoData != null) {
                            myOrgInfoDataList = myOrgInfoData.getData();
                            setPoPuData(myOrgInfoDataList);
                        }
                    } catch (Exception e) {
                        Log.e(TAG, "Exception==" + e.getMessage());
                    }

                } else {
                    ToastTool.showText(getApplicationContext(), "获取机构信息为空！");
                }
            }
        });
    }

    @Override
    public void clearShopData(String succ) {
        if ("0".equals(succ)) {
            clearListData();
        } else {
            Intent intent = new Intent();
            intent.setAction(Constants.BroadCastAction.FINISH_ALL_ACTIVITY);
            sendBroadcast(intent);
            finish();
        }

    }

    @Override
    public void refreshData() {

    }
}